﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NHibernate.Cfg;

using NHibernate;
using XWord.Fundamental.Domain;
using NHibernate.Tool.hbm2ddl;
using System.Data.SqlClient;
using NHibernateUnitOfWork;
using Luna.NHibernate;
using XWord.Fundamental.Data.Impl;
using XWord.Fundamental.Data.Repositories;
using System.Collections;
using Iesi.Collections.Generic;
using Iesi.Collections;
using System.Net;

namespace ConsoleApplication1
{
   

    class Program
    {
       
        static void Main(string[] args)
        {
            Configuration config = UnitOfWork.Configuration;

            string a = AppDomain.CurrentDomain.BaseDirectory;

            config.Properties[NHibernate.Cfg.Environment.ConnectionString] = @"data source=D:\develop\wpf\XWord2\trunk\XWord.GUI\bin\Debug\a.sqlite";

            UnitOfWork.Start();


            char[] chars = new char[26];
            for (int i = 0; i <= 25; i++)
            {
                Alphabet bet = new Alphabet();
                chars[i] = (char)(i + 65);
                bet.Name = chars[i].ToString();
                bet.Etymon = string.Empty;
                bet.PronounceRule = string.Empty;
                UnitOfWork.CurrentSession.Save(bet);
                UnitOfWork.CurrentSession.Flush();
            }
            //ImportReadTest.Test2();
      
            return;
           // ImportReadTest.Test1();
            
           //// GapFillTest.Test1();
           //// GapFillTest.Test3();
           //// QuestionTest.Test3();
           // //QuestionTest.Test4();
           // //return;
           // WordRepository rp = new WordRepository();
           // var list = rp.LoadWordList().ToList();
           // for (int i = 0; i < list.Count; i++)
           // {

           //     Console.ForegroundColor = ConsoleColor.Red;
           //     var w = list[i];
           //     Console.WriteLine(i + " " + w.Name);
           //     if (!string.IsNullOrEmpty(w.Audio))
           //         Downloadfile(w.Audio, a + "Resources\\" + w.Name + ".mp3");
           //     //Console.ForegroundColor = ConsoleColor.White;
           //     //var dd = NetWord.aa(w.Name,w);
           //     //if (!string.IsNullOrEmpty(dd["def"].ToString()))
           //     //{
           //     //    w.Comment = dd["def"].ToString();
           //     //    w.Audio = dd["audio"].ToString();

           //     //    w.Sentences.AddAll(dd["sentence"] as ICollection);
           //     //    rp.SaveWord(w);
           //     //}
           // }
     
           // NetWord
            //EtymaRepository rp = new EtymaRepository();
            //var av = rp.GetEtymaList<Etymon>();
            //Configuration config = new Configuration();
            //config.Configure();
            //string a = AppDomain.CurrentDomain.BaseDirectory;
            //config.Properties[NHibernate.Cfg.Environment.ConnectionString] = string.Format(config.Properties[NHibernate.Cfg.Environment.ConnectionString], a);
            //ISessionFactory sf = config.BuildSessionFactory();
            //ISession session = sf.OpenSession();
           
            //new SchemaExport(config).Execute(false, true, false);

            //DataTranslate.Run(session);           
        }

        /// <summary>
        /// 下载该URL的资源到本地
        /// </summary>
        /// <param name="URL">要下载的资源的地址</param>
        /// <param name="savePath">本地要保存的路径</param>
        public static void Downloadfile(string URL, string savePath)
        {
            WebClient client = new WebClient();
            int n = URL.LastIndexOf('/');
            string URLAddress = URL.Substring(0, n);  //取得网址
            string fileName = URL.Substring(n + 1, URL.Length - n - 1);  //取得文件名
            //string Dir =Htppc Server.MapPath("./");  //下载文件存放路径

            string Path = savePath + '\\' + fileName; //下载文件存放完整路径
            try
            {
                client.DownloadFile(URL, savePath);
            }
            catch (Exception)
            {
                
               // throw;
            }
            
        }
    }
}
